package com.xqls.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.xqls.bean.UserBean;
import com.xqls.biz.UserBiz;
import com.xqls.util.CommonResult;

@Controller
@RequestMapping("/userController")
public class UserController {
    @Autowired private UserBiz userBiz;
    
    /**
     * 插入账户信息
     * @param userBean
     * @return JSON
     */
    @RequestMapping("/insertUser")
    @ResponseBody
    public String insertUser(@RequestBody UserBean userBean){
    	CommonResult commonResult = this.userBiz.insertUser(userBean);
    	return JSONObject.toJSONString(commonResult);
    }
    
    /**
     * 修改账户信息
     * @param userBean
     * @return JSON
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateUser(@RequestBody UserBean userBean){
    	CommonResult commonResult = this.userBiz.updateUser(userBean);
    	return JSONObject.toJSONString(commonResult);
    }
    
    /**
     * 根据条件，修改账户信息
     * @param userBean
     * @return JSON
     */
    @RequestMapping("/updateUserSelective")
    @ResponseBody
    public String updateUserSelective(@RequestBody UserBean userBean){
    	CommonResult commonResult = this.userBiz.updateUserSelective(userBean);
    	return JSONObject.toJSONString(commonResult);
    }
    
    /**
     * 删除账户信息
     * @param userId
     * @return
     */
    @RequestMapping("/deleteUser/{userId}")
    @ResponseBody
    public String deleteUser(@PathVariable String userId){
    	CommonResult commonResult = this.userBiz.deleteUser(userId);
    	return JSONObject.toJSONString(commonResult);
    }
    
    /**
     * 根据账户名称，获得账户信息
     * @param userName
     * @return ModelAndView
     */
    @RequestMapping("/getUser/{userName}")
    public ModelAndView getUserByName(@PathVariable String userName){
    	ModelAndView view=new ModelAndView("/user/userEdit");
		view.addObject("userBean", this.userBiz.getUserByName(userName));
    	return view;
    }
}
